//
//	 ______    ______    ______    
//	/\  __ \  /\  ___\  /\  ___\   
//	\ \  __<  \ \  __\_ \ \  __\_ 
//	 \ \_____\ \ \_____\ \ \_____\ 
//	  \/_____/  \/_____/  \/_____/ 
//
//	Powered by BeeFramework
//
//
//  SearchActivityBoard.m
//  iHeima
//
//  Created by he chao on 6/5/14.
//  Copyright (c) 2014 leon. All rights reserved.
//

#import "SearchActivityBoard.h"
#import "ActivityDetailBoard.h"
#import "ActivityCell.h"

#pragma mark -

@interface SearchActivityBoard()
{
	//<#@private var#>
    
    BeeUITextField* _fieldSearch;
    
}
@end

@implementation SearchActivityBoard
DEF_SIGNAL(CANCEL)

- (void)load
{
}

- (void)unload
{
}

#pragma mark - Signal

ON_CREATE_VIEWS( signal )
{
    [self showNaviBar];
    [self loadContent];
}

ON_DELETE_VIEWS( signal )
{
}

ON_LAYOUT_VIEWS( signal )
{
}

ON_WILL_APPEAR( signal )
{
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    if (!fieldSearch) {
        fieldSearch = [BeeUITextField spawn];
        fieldSearch.frame = CGRectMake(10, 7, 250, 30);
        fieldSearch.backgroundColor = RGB(244, 244, 244);
        BeeUIImageView *imgSearchIcon = [BeeUIImageView spawn];
        imgSearchIcon.frame = CGRectMake(0, 0, 35, 30);
        imgSearchIcon.image = IMAGESTRING(@"search");
        fieldSearch.leftView = imgSearchIcon;
        fieldSearch.leftViewMode = UITextFieldViewModeAlways;
        fieldSearch.returnKeyType = UIReturnKeySearch;
        fieldSearch.layer.cornerRadius = 3;

        fieldSearch.placeholder=@"黑马";
    }
    [self.navigationController.navigationBar addSubview:fieldSearch];
    
    if (!btnCancel) {
        btnCancel = [BaseButton spawn];
        btnCancel.frame = CGRectMake(260, 0, 60, 44);
        [btnCancel setTitle:@"取消" forState:UIControlStateNormal];
        [btnCancel setTitleColor:kBlueColor forState:UIControlStateNormal];
        [btnCancel addSignal:SearchActivityBoard.CANCEL forControlEvents:UIControlEventTouchUpInside];
    }
    [self.navigationController.navigationBar addSubview:btnCancel];
}

ON_DID_APPEAR( signal )
{
}

ON_WILL_DISAPPEAR( signal )
{
    
}

ON_DID_DISAPPEAR( signal )
{
    [fieldSearch removeFromSuperview];
    [btnCancel removeFromSuperview];
}

ON_SIGNAL3( BeeUINavigationBar, LEFT_TOUCHED, signal )
{
}

ON_SIGNAL3( BeeUINavigationBar, RIGHT_TOUCHED, signal )
{
}

ON_SIGNAL2(SearchActivityBoard, signal){
    if ([signal is:SearchActivityBoard.CANCEL]) {
        [self.navigationController dismissViewControllerAnimated:YES completion:nil];
    }
}

ON_SIGNAL2(BeeUITextField, signal) {
    if ([signal is:BeeUITextField.RETURN]) {
        if (fieldSearch.text.length == 0) {
            [[BeeUITipsCenter sharedInstance] presentMessageTips:@"请输入搜索关键字"];
            return;
        }
        
        BeeHTTPRequest *request = [self POST:[NSString stringWithFormat:@"%@/apiactivity/search/",kBaseUrl]].PARAM(@"uid",kUserInfo[@"uid"]).PARAM(@"keywords",fieldSearch.text);
        request.tag = 9529;
    }
}

- (void)handleRequest:(BeeRequest *)request
{
    if(request.failed)
    {
        [[BeeUITipsCenter sharedInstance] dismissTips];
        NETWORK_ERROR
    }
    else if (request.succeed)
    {
        id json = [request.responseString mutableObjectFromJSONString];
        NSLog(@"%@",json);
        switch ([json[@"status"] intValue]) {
            case 1:
            {
                arrayActivities = json[@"res"];
                [myTableView reloadData];
            }
                break;
        }
    }
}

- (void)loadContent{
    
    UITapGestureRecognizer* tap  = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(resigh)];
    [self.view addGestureRecognizer:tap];
    
    myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.viewWidth, self.viewHeight-(IOS7_OR_LATER?64:44))];
    myTableView.delegate = self;
    myTableView.dataSource = self;
    [self.view addSubview:myTableView];
    
    
}


- (void)resigh{
    [fieldSearch endEditing:YES];

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [arrayActivities count];
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 80;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ActivityCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (nil == cell) {
        cell = [[ActivityCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        [cell initSelf];
    }
    cell.backgroundColor = [UIColor clearColor];
    //cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.dictActivity = arrayActivities[indexPath.row];
    [cell load];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    ActivityDetailBoard *board = [[ActivityDetailBoard alloc] init];
    board.dictActivity = arrayActivities[indexPath.row];
    [self.stack pushBoard:board animated:YES];
}

@end
